package cc.yugu.yhdw.webserver.web.controller.security.hdw;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.constant.ConstEnum;
import cc.yugu.yhdw.common.domain.basic.User;
import cc.yugu.yhdw.common.domain.hdw.Supplier;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.service.hdw.SupplierService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping(value = "/security/hdw/supplier")
public class SupplierController extends SecurityController {
    @Autowired
    SupplierService supplierService;

    @ResponseBody
    @ViewModel(ViewModel.JSON_ARRAY)
    @RequestMapping(value = "tree.htm")
    public void tree(HttpServletResponse response) throws Exception {
        response.setContentType(ConstEnum.ContentType.JSON.getValue());
        supplierService.tree(response.getOutputStream());
    }
    @RequestMapping(value = "index.htm")
    public void index(Model model) {
        model.addAttribute("typeList", Supplier.Type.values());
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_01_03.getValue());
    }


    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(Supplier search,HttpSession httpSession) {
//        if (getSessionUser(httpSession).getType()!= User.Type.PLATFORM.getValue()&&getSessionUser(httpSession).getType()!= User.Type.SUPPLIER.getValue()){
//            return null;
//        }
        if (getSessionUser(httpSession).getType() == User.Type.SUPPLIER.getValue()){
            search.setId(getSessionUser(httpSession).getSupplierId());
        }
        return PageResult.successResult(supplierService.findPage(search));
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping("add.htm")
    public void add(Model model) {
        model.addAttribute("typeList", Supplier.Type.values());
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "edit.htm")
    public String edit(int id, Model model) {
        Supplier supplier = supplierService.find(id);
        model.addAttribute("entity", supplier);
        return "/security/hdw/supplier/edit";
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "create.htm")
    public ExtResult create(Supplier entity) {
        return supplierService.insert(entity);
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "update.htm")
    public ExtResult update(Supplier entity) {
        return supplierService.update(entity);
    }
    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "delete.htm")
    public ExtResult delete(int id,int activeFlag) {
        return supplierService.delete(id,activeFlag);
    }

    @RequestMapping(value = "view.htm")
    public void view(Model model, int id, Integer item) {
        model.addAttribute("id", id);
        model.addAttribute("item", item);
    }

    @RequestMapping(value = "view_basic_info.htm")
    public String viewBasicInfo(Model model, int id) {
        Supplier entity = supplierService.find(id);
        if (entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        return "/security/hdw/supplier/view_basic_info";
    }

}
